<?
##########################################################################
# 			     DOCUMENTATION
##########################################################################
# Module to upload new books in the project. It can handle files given
# directly but also create new files from data given through a textarea and
# some input tags.
#
# © LiberLab group, 2011
##########################################################################

require_once("dataStructure/book.php");
require_once("dataStructure/user.php");
require_once("dynamic/base/sendData.php");

session_start();

class Upload extends SendData{
	public function __construct() {
		parent::__construct("upload", "upload");
	}
	
	public function main(){
		$userClass = new User();
		$errorPage = "error.html";
		$okPage = "upload_results.html";
		$pageToLoad = "";
		
		$bookClass = new Book();
		
		if (!$this->checkAccess()){
			return $this->backOff();
		}
		
		# basic error, just in case (should never been used, though)
		$this->tagHash["content"] = "<h1 lang=\"en\" xml:lang=\"en\">Error</h1>\n<p lang=\"en\" xml:lang=\"en\">ERROR, GENERIC.</p>";
		
		$authors = "";
		$authorId = NULL;
		$authorName = NULL;
		$authorSurname = NULL;
		$authorsArray = array();
		$authorMyself = getParam("author_myself", 0);
		if ($authorMyself){
			# if I am the author, get the data from my profile
			$userData = $userClass->getUserData($this->getSession("nickname"), $this->getSession("password"));
			if ($userData and $userData->name and $userData->surname and $userData->surname != " " and $userData->name != " "){
				$authors = $userData->name . " " . $userData->surname;
				$authorName = $userData->name;
				$authorSurname = $userData->surname;
				$authorId = $userData->authorId;
			}else{
				$pageToLoad = $errorPage;
				$this->tagHash["content"] = $this->messages["noAuthor"];
			}
		}else{
			$authorName1 = getParam("author_name1", NULL);
			$authorSurname1 = getParam("author_surname1", NULL);
			if (!($authorName1 and $authorSurname1)){
				$pageToLoad = $errorPage;
				$this->tagHash["content"] = $this->messages["noAuthor"];
			}else{
				$authorName = $authorName1;
				$authorSurname = $authorSurname1;
				#$authorsArray[] = array("name" => $authorName, "surname" => $authorSurname);
				# the first author is inserted with the book!
				$authorName2 = getParam("author_name2", NULL);
				$authorSurname2 = getParam("author_surname2", NULL);
				$authorName3 = getParam("author_name3", NULL);
				$authorSurname3 = getParam("author_surname3", NULL);					
				$authors = $authorName1 . " " . $authorSurname1;
				if ($authorName2 and $authorSurname2){
					$authorsArray[] = array("name" => $authorName2, "surname" => $authorSurname2);
					$authors = $authors . ", " . $authorName2 . " " . $authorSurname2;
				}
				if ($authorName3){
					$authorsNames = explode(", ", $authorName3);
					$authorsSurnames = explode(", ", $authorSurname3);
					for($index = 0; $index < count($authorsNames); $index++){
						$name = $authorsNames[$index];
						$surname = $authorsSurnames[$index];
						$authorsArray[] = array("name" => $name, "surname" => $surname);
						$authors = $authors . ", " . $name . " " . $surname;
					}
				}
			}
		}
		
		$editorName = getParam("editor_name", NULL);
		$editorAddress = getParam("editor_address", NULL);
		$editorPhoneNumber = getParam("editor_phonenumber", NULL);
		$editorEmail = getParam("editor_email", NULL);
		if ($editorEmail and !$this->mailCheck($editorEmail)){
			$pageToLoad = $errorPage;
			$this->tagHash["content"] = $this->messages["editorEmail"];
		}
		# everything ok untill now!
		if (!$pageToLoad){
			# book already inserted?
			$bookId = $bookClass->exists($this->getSession("title"), $this->getSession("category"), $authors); 
			if ($bookId){
				$bookAlreadyInserted  = str_replace("__bookname__", $this->getSession("title"), $this->messages["bookAlreadyInserted"]);
				$this->tagHash["content"] = $bookAlreadyInserted;
				$pageToLoad = $errorPage;	
			}else{
				# insert the book
				$bookClass->bookInsert($this->getSession("title"), $this->getSession("description"), $this->getSession("category"), 
									   $this->getSession("content"), $this->getSession("userId"), 
									   $this->getSession("coverExtension"), $this->getSession("isbn"), 
									   $authorId , $authorName, $authorSurname, $editorName, 
									   $editorAddress, $editorPhoneNumber, $editorEmail);
				$bookId = $bookClass->exists($this->getSession("title"), $this->getSession("category"), $authors);
				foreach($authorsArray as $author){
					$userClass->authorInsert($this->getSession("userId"), $author["name"],  $author["surname"], NULL, $bookId);
				}					   
							
				$toDir = $this->imagePath . "/" . $bookId . "/";
				
				if (!file_exists($this->getSession("tmpCover"))){
						$this->tagHash["content"] = $this->messages["cantSaveToServer"];
						$pageToLoad = $errorPage;	
				}else{
					if (!file_exists($toDir)){
						// mkdir function doesn't work correctly, due to server's configuration
						system("mkdir \"$toDir\"");
					}
					copy($this->getSession("tmpCover"),  fromCurrentDir($this->imagePath . "/" . $bookId . 
											    "/cover." . $this->getSession("coverExtension")));
					unlink($this->getSession("tmpCover"));
					// fix permessions: they are wrong set in the database configuration.
					// when I asked the answer was to do in this way.
					system("chmod 755 -R \"" . $this->imagePath . "\"");
					$this->tagHash["content"] = $this->messages["uploadSucceded"];
					$pageToLoad = $okPage;
				}
			}
		}
		if ($pageToLoad == $errorPage){
			# add the comeBack javascript
			$this->tagHash["content"] = $this->tagHash["content"] . "\n" . $this->messages["comeBack"];
		}
		if (!$pageToLoad){
			$pageToLoad = $errorPage;
		}
		
		return $this->render($pageToLoad);
	}

	public function __destruct() {
		parent::__destruct();
	}
};

$upload = new Upload();
echo $upload->main();

?>
